package com.example.controller;

import com.example.domain.Examination;
import com.example.service.ExaminationService;
import com.example.utils.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.example.utils.AjaxResult.*;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/root")

public class ExaminationController {
    @Autowired
    private ExaminationService examinationService;

    /** 药品管理 */
    @RequestMapping("/examination")
    public AjaxResult Examination(@RequestParam(defaultValue = "1", value = "page") Integer pageNum,
                                   @RequestParam(defaultValue = "10", value = "limit") Integer pageSize,
                                   @RequestParam(defaultValue = "" , value = "examination") String usr, HttpServletRequest request
    ){
        int patient_id =(int) request.getSession().getAttribute("patient_id");
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        if (usr == null || usr.equals("")) {
            map.put("usr","");
            List<Examination> examination = examinationService.listAll(patient_id);
            System.out.println(examination.toString());
            PageInfo<Examination> page = new PageInfo<>(examination);
            return successData(page.getTotal(), examination);
        }
        map.put("usr",usr);
        List<Examination> examination = examinationService.listAll(patient_id);
        PageInfo<Examination> page = new PageInfo<>(examination);
        return successData(page.getTotal(), examination);
    }

    /** 检验管理 */
    @RequestMapping("/examination2")
    public AjaxResult Examination2(@RequestParam(defaultValue = "1", value = "page") Integer pageNum,
                                  @RequestParam(defaultValue = "10", value = "limit") Integer pageSize,
                                  @RequestParam(defaultValue = "" , value = "examination2") String usr, HttpServletRequest request
    ){
//        int patient_id =(int) request.getSession().getAttribute("patient_id");
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        if (usr == null || usr.equals("")) {
            map.put("usr","");
            List<Examination> examination = examinationService.listAll(1);
            System.out.println(examination.toString());
            PageInfo<Examination> page = new PageInfo<>(examination);
            return successData(page.getTotal(), examination);
        }
        map.put("usr",usr);
        List<Examination> examination = examinationService.listAll(1);
        PageInfo<Examination> page = new PageInfo<>(examination);
        return successData(page.getTotal(), examination);
    }

    /** 新增添加 */
    @RequestMapping("/addExam")
    public AjaxResult addExam(Examination examination){
        System.out.println("test1");
        /*        System.out.println(examination.getIll_case());*/
        int insert = examinationService.insert(examination);
        if (1 == insert){
            return success("添加成功");
        }
        return error("添加失败");
    }

    /** 检验修改 */
    @RequestMapping("/modifyExam")
    public AjaxResult modifyExam(Examination examination){

        int update = examinationService.update(examination);

        if (1 == update){
            return success("修改成功");
        }
        return error("修改失败");
    }
}
